window.onload = function () {
    const token = sessionStorage.getItem("token");
    axios({
        url: "http://localhost:8080/admin/aadmin/queryAdmin",
        method: "post",
        Headers: { token }
    })
        .then(res => {
            if (res.data.code == 200) {
                //存储管理员信息
                sessionStorage.setItem("adminInfo", JSON.stringify(res.data.data));
                 console.log(res.data.data);
                adminRender(res.data.data)

            } else {

            }
        })
}

//封装渲染
function adminRender(data) {
    const tbody = document.querySelector("tbody")
    tbody.innerHTML = ""
    data.forEach((item, index) => {
        let str = `
        <tr>
            <td>${index + 1}</td>
            <td>${item.name}</td>
            <td>${item.password}</td>
            <td>
                <button class="btn btn-danger btn-sm" data-id=${item.id}>删除</button>
                <button class="btn btn-warning btn-sm" data-id=${item.id} data-bs-toggle="modal" data-bs-target="#updateAdmin">修改</button>
            </td>
        </tr>
        `
        tbody.innerHTML += str;

    });
}

//事件委托
const tbody = document.querySelector("tbody");
tbody.addEventListener("click", (e) => {
    //删除管理员信息
    if (e.target.className.includes("danger")) {
        const res = window.confirm("您确定要删除吗？")
        if (res) {
            const token = sessionStorage.getItem("token");
            const id = e.target.dataset.id;
            axios({
                url: "http://localhost:8080/admin/aadmin/delAdmin",
                method: "post",
                Headers: { token },
                data: { id }
            })
                .then(res => {
                    location.reload();
                })
        }
    }
    //修改管理员信息
    if (e.target.className.includes("warning")) {
        const token = sessionStorage.getItem("token");
        const id = e.target.dataset.id;
        axios({
            url: "http://localhost:8080/admin/aadmin/queryAdminById",
            method: "post",
            Headers: { token },
            data: { id }
        })
            .then(res => {
                console.log(res.data.data);
                document.getElementById("id").value = res.data.data.id;
                document.getElementById("num").value = res.data.data.name;
                document.getElementById("pass").value = res.data.data.password;
            })
    }
})

//修改管理员信息
function updateAdmin() {
    const id = document.getElementById("id").value
    const name = document.getElementById("num").value
    const password = document.getElementById("pass").value
    const token = sessionStorage.getItem("token")
    //非空判断
    if (!(name.length && password.length)) {
        alert("账号密码不能为空")
    } else {
        axios({
            url: "http://localhost:8080/admin/aadmin/updateAdmin",
            method: "post",
            Headers: { token },
            data: { id, name, password }
        })
            .then(res => {
                location.reload();
            })
    }
}

//添加管理员信息
function addAdmin() {
    const name = document.getElementById("adminnum").value
    const password = document.getElementById("adminpass").value
    const token = sessionStorage.getItem("token")
    //非空判断
    if (!(name.length && password.length)) {
        alert("账号密码不能为空")
    } else {
        axios({
            url: "http://localhost:8080/admin/aadmin/addAdmin",
            method: "post",
            Headers: { token },
            data: { name, password}
        })
            .then(res => {
                location.reload();
            })
    }
}

//模糊查询
function adminByLikeName(){
    const name = document.getElementById("likename").value.trim()
    const token = sessionStorage.getItem("token");
    axios({
        url: "http://localhost:8080/admin/aadmin/likename",
        method: "post",
        Headers: { token },
        data: { name }
    })
    .then(res => {
        adminRender(res.data.data)
    })
}

